package cc.vicp.djx314.starfood.web.personnel.order;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cc.vicp.djx314.starfood.domain.order.Order;
import cc.vicp.djx314.starfood.service.exception.OrderNotFoundException;
import cc.vicp.djx314.starfood.service.order.CurrentOrder;
import cc.vicp.djx314.starfood.service.order.OrderService;

@Controller
public class GetOrderInfoController {
	public static final Logger logger = LoggerFactory.getLogger(GetOrderInfoController.class);

	@Autowired
	private OrderService orderService;

	@Autowired
	private CurrentOrder currentOrder;

	@RequestMapping(value = "/personnel/currentorder", method = RequestMethod.GET)
	public ModelAndView getCurrentOrderInfo() {
		boolean success = false;
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		Order order = null;
		try {
			order = orderService.getCurrentOrderForPersonnel();
			success = true;
		} catch (Exception e) {

		}
		model.addObject("order", order);
		model.addObject("success", success);
		return model;
	}

	@RequestMapping(value = "/personnel/order/{id}", method = RequestMethod.GET)
	public ModelAndView getOrderInfoById(@PathVariable("id") Integer orderId)
			throws OrderNotFoundException {
		Order order = orderService.getOrderForPersonnel(orderId);
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		if (order.getOrderCancled() == true) {
			model.addObject("orderCancled", true);
			model.addObject("success", true);
		} else if (order.getOrderEnabled() == true) {
			model.addObject("orderEnabled", true);
			model.addObject("success", true);
		} else {
			model.addObject("order", order);
			currentOrder.addCurrnetOrder(order.getId());
			model.addObject("success", true);
		}
		return model;
	}
}